package leetcode_top;

import java.util.*;
import org.junit.*;

public class Ex150 {
    class Solution {
        public int evalRPN(String[] tokens) {
            Stack<Integer> nums = new Stack<>();
            int b, a;
            for (int i = 0; i < tokens.length; i++) {
                switch (tokens[i]) {
                    case "+":
                        b = nums.pop();
                        a = nums.pop();
                        nums.push(a + b);
                        break;
                    case "-":
                        b = nums.pop();
                        a = nums.pop();
                        nums.push(a - b);
                        break;
                    case "*":
                        b = nums.pop();
                        a = nums.pop();
                        nums.push(a * b);
                        break;
                    case "/":
                        b = nums.pop();
                        a = nums.pop();
                        nums.push(a / b);
                        break;
                    default:
                        nums.push(Integer.parseInt(tokens[i]));
                }
            }
            return nums.peek();
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.evalRPN(new String[]{"2","1","+","3","*"}));        
    }
}
